﻿using System;
using System.Drawing;
using System.Resources;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace CoreTypes
{
   public static partial class ResourceWrapper
   {
      static ResourceManager FDefaultManager;
      static StringDictionary FResourceAliases;
      static Collection<ResourceManager> FResourceManagerCollection;

      static ResourceWrapper()
      {
         FResourceAliases = new StringDictionary();
         FResourceManagerCollection = new Collection<ResourceManager>();

         DefaultManager = CoreTypes.Properties.Resources.ResourceManager;
      }

      public static void AddResourceManager(ResourceManager resourceManager, bool setDefault)
      {
         if (resourceManager == null) return;

         if (!FResourceManagerCollection.Contains(resourceManager)) FResourceManagerCollection.Add(resourceManager);

         if (setDefault) FDefaultManager = resourceManager;
      }

      public static void SetResourceAlias(string alias, string resource)
      {
         if (GetObject(resource) == null) return;

         FResourceAliases[alias] = resource;
      }

      public static ResourceManager DefaultManager
      {
         get
         {
            return FDefaultManager;
         }
         set
         {
            if (value == null) return;

            if (!FResourceManagerCollection.Contains(value)) FResourceManagerCollection.Add(value);

            FDefaultManager = value;
         }
      }

      public static object GetObject(string name)
      {
         object obj = FDefaultManager.GetObject(name);

         if (obj == null)
         {
            foreach (ResourceManager mgr in FResourceManagerCollection)
            {
               if (mgr == FDefaultManager) continue;

               obj = mgr.GetObject(name);

               if (obj != null) break;
            }
         }

         return obj;
      }

      public static String GetString(string name)
      {
         return (string)GetObject(name);
      }

      public static Bitmap GetImage(string name)
      {
         Bitmap image = (Bitmap)GetObject(FResourceAliases.ContainsKey(name) ? FResourceAliases[name] : name);

         image.MakeTransparent(Color.Magenta);

         return image;
      }

      public static Bitmap Warning
      {
         get
         {
            return GetImage("Warning");
         }
      }

      public static Bitmap EmptyBitmap
      {
         get
         {
            return new Bitmap(16, 16);
         }
      }

      public static Bitmap CreateAccount
      {
         get
         {
            return GetImage("NewAccount");
         }
      }

      public static Bitmap RemoveAccount
      {
         get
         {
            return GetImage("DropAccount");
         }
      }

      public static Bitmap Session
      {
         get
         {
            return GetImage("Session");
         }
      }

      public static Bitmap SessionUserSelected
      {
         get
         {
            return GetImage("SessionUserSelected");
         }
      }

      public static Bitmap SayingBubble
      {
         get
         {
            return GetImage("SayingBubble");
         }
      }

      public static Bitmap SayingBubbleBlue
      {
         get
         {
            return GetImage("SayingBubbleBlue");
         }
      }

      public static Bitmap PanelBackground
      {
         get
         {
            return GetImage("PanelBackground");
         }
      }

      public static Bitmap ContactListUser
      {
         get
         {
            return GetImage("ContactListUser");
         }
      }

      public static Bitmap TreeViewMessageNotify
      {
         get
         {
            return GetImage("TreeViewMessageNotify");
         }
      }

      public static Bitmap TreeViewContactListUser
      {
         get
         {
            return GetImage("TreeViewContactListUser");
         }
      }

      public static Bitmap TreeViewContactListUserSelected
      {
         get
         {
            return GetImage("TreeViewContactListUserSelected");
         }
      }

      public static Bitmap IncomingMessageBackground
      {
         get
         {
            return GetImage("IncomingMessageBackground");
         }
      }

      public static Bitmap OutgoingMessageBackground
      {
         get
         {
            return GetImage("OutgoingMessageBackground");
         }
      }

      public static Bitmap SessionVisualizerViewEntryHeaderBackground
      {
         get
         {
            return GetImage("SessionVisualizerViewEntryHeaderBackground");
         }
      }
   }
}

